//
//  SYPictureView.swift
//  SYWeibo
//
//  Created by Saya on 16/4/13.
//  Copyright © 2016年 黄金果. All rights reserved.
//

import UIKit
import SDWebImage

private let ID:String = "collectionCell"
private let marginBtItem:CGFloat = 5
private let itemWidth:CGFloat = (screenWidth - marginForCell*2 - marginBtItem*2) / 3
private let maxViewWidth:CGFloat = screenWidth - marginForCell*2

class SYPictureView: UICollectionView,UICollectionViewDataSource {
    
    //数据在这边
    var pic_urls:[SYPictureModel]?{
        didSet{
            numberLabel.text = "\(pic_urls?.count ?? 0)"
    
            if let tempPicArray = pic_urls {
                let itemSize:CGSize  = caculateViewSize(tempPicArray)
                self.snp_updateConstraints { (make) -> Void in
                    make.size.equalTo(itemSize)
                }
                
                reloadData()
            }
        }
    }
    
    private func caculateViewSize(pic_urls:[SYPictureModel])->CGSize{
        if pic_urls.count == 0{
            return CGSizeZero
        }
        
        if pic_urls.count == 1{
            let tempSize:CGSize = CGSize(width: 170, height: 120)
            if let layout = self.collectionViewLayout as? UICollectionViewFlowLayout{
                layout.itemSize = tempSize
            }
            return tempSize
        }
        
        
        if let layout = self.collectionViewLayout as? UICollectionViewFlowLayout {
            layout.itemSize = CGSize(width: itemWidth, height: itemWidth)
        }
        
        
        if pic_urls.count == 4{
            let width :CGFloat = itemWidth*2 + marginForCell
            return CGSize(width: width, height: width)
        }
        
        if pic_urls.count == 2{
            let width :CGFloat = itemWidth*2 + marginForCell
            return CGSize(width: width, height: itemWidth)
        }
        
        let row = ((pic_urls.count - 1) / 3) + 1
        let rowF = CGFloat(row)
        let rowHeight = rowF*itemWidth + (rowF - 1)*marginBtItem
        return CGSize(width: maxViewWidth, height: rowHeight)
    }
    
    
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        let flowLayout = UICollectionViewFlowLayout()
        super.init(frame: frame, collectionViewLayout: flowLayout)
        
        setUpFlowLayout(flowLayout)
        setUpUI()
        
        backgroundColor = UIColor.blueColor()
    }
    
    private func setUpFlowLayout (flowLayout:UICollectionViewFlowLayout){
        
        self.snp_makeConstraints { (make) -> Void in
            make.size.equalTo(CGSize(width: 100, height: 100))
        }
        self.dataSource = self
        registerClass(SYCellForPicture.self, forCellWithReuseIdentifier: ID)
        flowLayout.minimumInteritemSpacing = marginBtItem
        flowLayout.minimumLineSpacing = marginBtItem
        flowLayout.itemSize = CGSize(width: itemWidth, height: itemWidth)
    }



    private func setUpUI(){
        addSubview(numberLabel)
        
        numberLabel.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self.center)
        }
    }
    
    lazy var numberLabel:UILabel = UILabel(text: "0", font: 15, textColor: UIColor.redColor())

    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

extension SYPictureView{
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return pic_urls?.count ?? 0
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(ID, forIndexPath: indexPath) as! SYCellForPicture
        if let tempPic = pic_urls{
            cell.picture = tempPic[indexPath.row]
        }
        return cell
    }
}

class SYCellForPicture: UICollectionViewCell {
    var picture:SYPictureModel?{
        didSet{
            if let tempUrlString = picture?.thumbnail_pic  {
                cellImg.sd_setImageWithURL(NSURL(string: tempUrlString))
            }
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        addSubview(cellImg)
        cellImg.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(self)
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
  
    lazy var cellImg:UIImageView = {
        let tempImgView = UIImageView()
        //修改图片的填充模式
        tempImgView.contentMode = .ScaleAspectFill
        //设置了ScaleAspectFill如果不加上clipsToBounds图片会超出边框显示
        tempImgView.clipsToBounds = true
        return tempImgView
    }()
}
